from django.db import models
from django.forms import ModelForm
from datetime import date
from documents.models import *
from Manager.calverter import Calverter



class InsuranceCompany(models.Model):
 	arabic_name = models.CharField(max_length=100)
 	name = models.CharField(max_length=100)
 	def __unicode__(self):
    		return self.name


class InsuranceType(models.Model):
    name = models.CharField(max_length=10)
    long_name = models.CharField(max_length=100)
    def __unicode__(self):
        return self.name

class CarModel(models.Model):
    name = models.CharField(max_length=100)
    arabic_name = models.CharField(max_length=100)
    def __unicode__(self):
        return self.name

class CarBrand(models.Model):
    name = models.CharField(max_length=100)
    
    arabic_name = models.CharField(max_length=100)
    def __unicode__(self):
        return self.name
       
class BrandForm(ModelForm):
   class Meta:
       model = CarBrand



class Car(models.Model):
    brand = models.ForeignKey(CarBrand)
    car_model = models.CharField(max_length=30)
    chase_no = models.IntegerField(max_length=20, null=True)

    year = models.IntegerField(default=0, null=True)
    user = models.OneToOneField('Person.Person', blank=True, null=True)
    company = models.OneToOneField('company.Company', blank=True, null=True)
    plate_str = models.CharField(max_length=10,blank=False)
    plate_number = models.IntegerField(max_length=10, blank=False, null=True)
    arabic_plate_str = models.CharField(max_length=10,blank=False, null=True)

    expiry_date = models.DateField(null=True, blank=True)

    arabic_reg_day = models.IntegerField(blank= True, null=True, max_length=2)
    arabic_reg_month = models.IntegerField(blank= True, null=True, max_length=2)
    arabic_reg_year = models.IntegerField(blank= True, null=True, max_length=4)


    checkup_expiry = models.DateField(null=True, blank=True)

    arabic_check_day = models.IntegerField(blank= True, null=True, max_length=2)
    arabic_check_month = models.IntegerField(blank= True, null=True, max_length=2)
    arabic_check_year = models.IntegerField(blank= True, null=True, max_length=4)


    insurance = models.ForeignKey(InsuranceCompany, blank=True, null=True, default=None)
    insurance_start = models.DateField(null=True, blank=True)
    insurance_end = models.DateField(null=True, blank=True)


    insurance_type = models.ForeignKey(InsuranceType, blank=True)
    insurance_cost = models.FloatField(null=True, blank=True)

    documents = models.ManyToManyField(Document , null=True, blank=True)


    def arabic_to_gorg_reg(self):
        cal = Calverter()
        jd = cal.islamic_to_jd(self.arabic_reg_year, self.arabic_reg_month,self.arabic_reg_day)
        [year1, month1, day1] = cal.jd_to_gregorian(jd)
        ds = date(month = month1, day = day1, year=year1)
        self.expiry_date = ds
    def arabic_to_gorg_check(self):
        cal = Calverter()
        jd = cal.islamic_to_jd(self.arabic_check_year, self.arabic_check_month,self.arabic_check_day)
        [year1, month1, day1] = cal.jd_to_gregorian(jd)
        ds = date(month = month1, day = day1, year=year1)
        self.checkup_expiry = ds

    def __unicode__(self):
        return self.brand + " " + self.car_model +  " " +self.arabic_plate_str

    def cal_exp(self):
        today = date.today()
        yesterday = self.expiry_date
        if(today > yesterday):
            return 0
        days =abs( today.day - yesterday.day )
        months = abs(today.month - yesterday.month) * 30
        years = abs(today.year - yesterday.year) * 356
        return years+months+days

    def cal_inssurance(self):
        today = date.today()
        yesterday = self.insurance_end
        if(today > yesterday):
          return 0
        days =abs( today.day - yesterday.day )
        months = abs(today.month - yesterday.month) * 30
        years = abs(today.year - yesterday.year) * 356
        return years+months+days
    def cal_checkup(self):

        today = date.today()
        yesterday = self.checkup_expiry
        if(today > yesterday):
            return 0

        days =abs( today.day - yesterday.day )
        months = abs(today.month - yesterday.month) * 30
        years = abs(today.year - yesterday.year) * 356
        return years+months+days


class CarForm(ModelForm):
    class Meta:
        model = Car

